function isScrolledIntoView(selector){
	var elem= $(selector);
	
	if(elem.length>0){
		var docViewTop = $(window).scrollTop();
	    var docViewBottom = docViewTop + $(window).height();

	    var elemTop = elem.offset().top;
	    var elemBottom = elemTop + elem.height();

	    return ( (elemBottom >= docViewTop) && (elemTop <= docViewBottom) && (elemBottom <= docViewBottom) &&  (elemTop >= docViewTop) );
	}
	
    return false;
}


var selector_next_posts= '.cl_nav .cl_link:visible .cl_next';

function getMorePosts(){
	$(selector_next_posts).click();
}

$(document).ready(function(){
	/***************/
	/** COLOR BOX **/
	/***************/
	
	//Doc => http://www.jacklmoore.com/colorbox
	
	$('[slide=yes]').colorbox({
		transition:"elastic", 
		scalePhotos:true, 
		maxWidth:"90%", 
		maxHeight:"90%",
		fixed:true,
		opacity: 0.60,
		current: '{current}/{total}',
		returnFocus: false
	});
	
	var timerResizeColorbox;	
	$(window).bind('resize orientationchange', function() {
		clearTimeout(timerResizeColorbox);
		timerResizeColorbox= setTimeout(function(){
			if($('#cboxOverlay:visible').length>0)
				$.fn.colorbox.load();
		}, 250);		
	});	
	
	/***********************/
	/** LOAD MORE CONTENT **/
	/***********************/
	
	//On scroll event
	$(function(){
		$(window).bind('scroll resize orientationchange', function(){
			if(isScrolledIntoView(selector_next_posts)){
				getMorePosts();
			}
			
			if(isScrolledIntoView('[role=footer]')){
				$('#user_modal').stop(true, true).fadeOut();
			}else{
				$('#user_modal').stop(true, true).fadeIn();
			}			
		});
	});
	
	//On document ready
	if(isScrolledIntoView(selector_next_posts)){
		getMorePosts();
	}
});

